package com.nep.NEPG.controller;

import com.nep.NEPG.model.AQIData;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;

public class AdminDataController {
    // FXML注入的UI组件
    @FXML private TableView<AQIData> dataTable;
    @FXML private Label statusLabel;
    @FXML private Label updateTimeLabel;

    // 数据集合
    private final ObservableList<AQIData> dataList = FXCollections.observableArrayList();

    @FXML
    public void initialize() {
        // 初始化表格列绑定
        setupTableColumns();

        // 加载初始数据
        refreshData();

        // 设置初始状态
        statusLabel.setText("系统就绪");
        updateTimeLabel.setText(getCurrentTime());
    }

    private void setupTableColumns() {
        // 确保列名与AQIData类的属性名完全匹配
        dataTable.getColumns().get(0).setCellValueFactory(new PropertyValueFactory<>("areaName"));
        dataTable.getColumns().get(1).setCellValueFactory(new PropertyValueFactory<>("aqiValue"));
        dataTable.getColumns().get(2).setCellValueFactory(new PropertyValueFactory<>("pm25"));
    }

    @FXML
    private void refreshData() {
        try {
            // 模拟数据加载
            dataList.clear();
            dataList.add(new AQIData("1", "T001", "A001", "朝阳区", 45.2, 35.1, 10.2, 8.5, 12.3, 0.8, false));
            dataList.add(new AQIData("2", "T002", "A002", "海淀区", 38.7, 42.3, 9.1, 7.8, 15.6, 0.9, true));

            dataTable.setItems(dataList);
            updateTimeLabel.setText(getCurrentTime());
            statusLabel.setText("数据刷新成功");
        } catch (Exception e) {
            statusLabel.setText("数据加载失败: " + e.getMessage());
        }
    }

    @FXML
    private void exportData() {
        // 实现导出逻辑
        statusLabel.setText("数据已导出");
        updateTimeLabel.setText(getCurrentTime());
    }

    private String getCurrentTime() {
        return java.time.LocalDateTime.now().format(
                java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }
}